package com.zhouyu.jdk;

import com.zhouyu.service.UserInterface;
import com.zhouyu.service.UserService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkTest {
	public static void main(String[] args) {


		UserService target = new UserService();

		// UserInterface接口的代理对象
		Object proxy = Proxy.newProxyInstance(UserService.class.getClassLoader(), new Class[]{UserInterface.class}, new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				System.out.println("before...");
				Object result = method.invoke(target, args);
				System.out.println("after...");
				return result;
			}
		});

		//获取代理对象，接口类型UserInterface
		UserInterface userService = (UserInterface) proxy;
		userService.test();

	}
}
